package org.playground.booking.controller;

import antlr.StringUtils;
import com.mchange.rmi.NotAuthorizedException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.playground.booking.dto.CoursList;
import org.playground.booking.dto.SportList;
import org.playground.booking.service.IPlaygroundService;
import org.playground.booking.utils.UnknownResourceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.nio.file.Path;
import java.util.Date;
import java.util.List;

/**
 * Created by root on 17-10-29.
 */
@Api
@RestController
@RequestMapping(path = "/api/playground/*")
public class PlaygroundController {
    @Autowired
    private IPlaygroundService playgroundService;


    @RequestMapping(path = "/sports",method = RequestMethod.GET,produces = "application/json")
    @ResponseBody
    @ApiOperation(value = "get sports list")
    public ResponseEntity<SportList> getSportList() {
        SportList sportList = playgroundService.getAllSports();
        return new ResponseEntity<SportList>(sportList, HttpStatus.OK);
    }


    @RequestMapping(path="/cours",method = RequestMethod.GET,produces = "application/json")
    @ResponseBody
    @ApiOperation(value = "get cours list")
    public ResponseEntity<CoursList> getCoursList(@RequestParam int sportId,
                                                  @RequestParam boolean includBookins,
                                                  @RequestParam boolean includMinutesForTime,
                                                  @RequestParam(required=false) Date date){

        if(date==null){
            //by default date is today, get today's booking
            date = new Date();
        }
        CoursList coursList = playgroundService.getCours(sportId,includBookins,includMinutesForTime,date);

        if(coursList==null){
            return new ResponseEntity<CoursList>(HttpStatus.NO_CONTENT);
        }

        return new ResponseEntity<CoursList>(coursList,HttpStatus.OK);
    }


    @RequestMapping(path="/merchant/cours", method = RequestMethod.GET,produces = "application/json")
    public ResponseEntity<List<CoursList>> getCoursListForMerchant(@RequestHeader("merchant_id") String merchantId,
                                                             @RequestParam(value = "sportId",required = false) @ApiParam( required = false) Integer sportId,
                                                             @RequestParam boolean includBookings,
                                                             @RequestParam boolean includMinutesForTime,
                                                             @RequestParam(required=false) Date date) throws NotAuthorizedException, UnknownResourceException {
        //TODO get  playgrounds for thie merchant
        if(org.springframework.util.StringUtils.isEmpty(merchantId)){
            throw new NotAuthorizedException("the merchantId is empty.");
        }
        Long  merchantIdL = Long.parseLong(merchantId);

        List<CoursList> response = playgroundService.getCoursForMerchant(merchantIdL,sportId, includBookings,includMinutesForTime,date);
        if(CollectionUtils.isEmpty(response)){
            throw new UnknownResourceException("No playground found for merchant : "+merchantId);
        }

        return new  ResponseEntity(response,HttpStatus.OK);
    }

}
